
var Grid = {
	tableId:"grid",
	dicsMap:null,
	options:{
		columns:[],
		toolbar:[],
		methord: 'get',
		url:'',
		idField:'',
		pageSize:10,
		pagination: true,
	    rownumbers: true,
	    singleSelect:true,
	    loader:myLoader,
	    fitColumns:true,
	    onLoadSuccess:onLoadSuccess
	},
	buttons:{
	    'add':{
	        text: '新增',
	        iconCls: 'icon-add'
	    },
	    'edit':{
	        text: '修改',
	        iconCls: 'icon-edit'
	    },
	    'remove':{
	        text: '删除',
	        iconCls: 'icon-remove'
	    }
	},
	initGrid:function(opts,tableId){
		Grid.options = $.extend({},Grid.options,opts);
//		options.columns = Grid.initColumn(options.columns);
		Grid.initColumn();
		Grid.options.height = $("#body").height()-$('#search_area').height()-5;
		Grid.options.width = $("#body").width();
		if(!tableId){
			tableId = Grid.tableId;
		}
		Grid.tableId = tableId;
		var grid = $('#'+Grid.tableId).datagrid(Grid.options);
		Grid.grid = grid;
//		$("table.datagrid-htable").attr("style","height: 25px;width:100%");
		return grid;
	},
	createToolBar:function(funcs,callBackFunc){
		if(!funcs){
			return ;
		}
		var _toolbar = [];
		for(var i=0;i<funcs.length;i++){
			var func = funcs[i];
			if(func.funcId=='back'&&callerPageURL==''){
				continue;
			}else{
			}
			var _tool = {
				text: func.dispName,
				iconCls: 'icon-'+func.funcId,
				handler: function(){var funcId = Grid.getToolIcon(this);callBackFunc(funcId);}
			}
			_toolbar.push(_tool);
		}
		return _toolbar;
	},
	getToolIcon:function(object){
		var _class = $(object).find("span.l-btn-icon").attr("class");
		_class = _class.replace("l-btn-icon","");
		_class = _class.replace("icon-","");
		_class = _class.replace(/ /g,"");
		return _class;
	},
	initColumn:function(){
		var _columns = [];
		_columns.push({ field: 'ID', checkbox: false,hidden:true });
		$.each(Grid.options.columns,function(i,k){
			var field = {}
			field.field = k.colName;
			field.title = k.dispName;
			field.sortable = true;
			if(k.isEdit==1){
				field.editor = {type:'validatebox',options:{required:true}};
			}
			field.formatter = function(value,row,index){
				return Grid.fieldFormat(k,value,row,index);
			}
			if(k.isPkey==1){
				var idField = Grid.options.idField;
				if(!idField){
					idField = field.field;
				}else{
					idField = idField+","+field.field;
				}
				Grid.options.idField = idField;
			}
			_columns.push(field);
		});
		Grid.options.columns = [_columns];
//		return [_columns];
	},
	reLoad:function(cndJson,grid){
		grid.datagrid('load',cndJson);
	},
	resize:function(){
		
	},
	fieldFormat:function(k,value,row,index){
		var dispValue = value;
		if(k.dicKind!=0&&Grid.dicsMap){
			dispValue = Grid.dicFormat(k,value);
		}
		if(k.firstExp){
			dispValue = Grid.firstExp(k.firstExp, value, row);
		}
		if(k.linkUrl){
			var linkUrl = System.replaceParam(k.linkUrl,k.paramFields,row);
			dispValue = "<a href=javascript:Report.handleLink('"+linkUrl+"','"+k.linkTarget+"','"+k.queryInfo+"')>"+dispValue+"</a>";
		}
		if(k.dispType==10){//带文本单选框
			var radioHtml = "<input type=\"radio\" name=\"col_"+k.colName+"\" value=\""+value+"\" onclick=\"javascript:Report.fireSetPopDic();\">";
			dispValue = radioHtml+dispValue;
		}
		if(k.isTagValue){
			dispValue += "<input type=\"hidden\" name=\"" + k.colName + "\" value=\"" + value + "\">";
		}
		return dispValue;
	},
	dicFormat:function(k,value){
		var dicKey = k.dicKind +"_"+k.dicGrpId;
		var dics = Grid.dicsMap[dicKey];
		var dispValue = System.parseDict(dics, value);
		if(!dispValue){
			dispValue = value;
		}
		return dispValue;
	},
	firstExp:function(firstExp,value,row){
		return firstExp;
	},
	maxLastWidth:function(){
		
	}
}
/*

Grid.reLoad = function(){
	grid.datagrid('load',cndJson);
});
*/

function myLoader(param,success,error){
	var that = $(this);
	var opts = that.datagrid("options");
	if (!opts.url) {
		return false;
	}
	System.ajax(opts.method,opts.url, param, function(data){
		success(data);
	});
	/*
	$.ajax({
		type : opts.method,
		url : opts.url,
		data : param,
		dataType : "json",
		success : function(data) {
			if(data.result=='error'){
				System.showError('错误', data.message);
				that.datagrid("loaded");
			}else{
				success(data);
			}
		},
		error : function() {
			error.apply(this, arguments);
		}
	});
	*/
}

function onLoadSuccess(data){
	var isEdit = Grid.grid.datagrid('options').isEdit;
	if(!isEdit){
		return;
	}
	var size = data.rows.length
	for(var index = 0;index<size;index++){
		Grid.grid.datagrid('beginEdit', index);
	}
}


